//
//  TopViewController.m
//  WXMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 无限互联. All rights reserved.
//

#import "TopViewController.h"
#import "MovieController.h"

@interface TopViewController ()

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, copy) NSMutableArray *dataArray;

@end

@implementation TopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _loadData];
    
   // _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    

    
   // _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:_flowLayout];
    [self _cofigUI];
    
    //_collectionView.delegate = self;
   // _collectionView.dataSource = self;
    
    //[_collectionView registerClass:[TopCollectionViewCell class] forCellWithReuseIdentifier:@"234"];
    
    //[self.view addSubview:_collectionView];
        // Do any additional setup after loading the view.
}

- (void)_cofigUI {
    //    @property (nonatomic) CGFloat minimumLineSpacing;
    //    @property (nonatomic) CGFloat minimumInteritemSpacing;
    //    @property (nonatomic) CGSize itemSize;
    //    @property (nonatomic) CGSize estimatedItemSize
    _flowLayout.itemSize = CGSizeMake(80, 100);
    _flowLayout.estimatedItemSize = CGSizeMake(20, 20);
    //NSLog(@"%@",NSStringFromCGSize(_flowLayout.estimatedItemSize));
    _flowLayout.minimumInteritemSpacing = 20;
    _flowLayout.minimumLineSpacing = 20;
    
}

- (void)_loadData{
    
    _dataArray = [NSMutableArray array];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"top250" ofType:@"json"]] options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = dic[@"subjects"];
    for (NSDictionary *dic in array) {
        
        NSDictionary *subject = [dic copy];
        TopModel *model = [[TopModel alloc] init];
        model.title = subject[@"title"];
        model.images = [subject[@"images"] objectForKey:@"large"];
        model.rating = [[subject[@"rating"] objectForKey:@"average"] floatValue];
        [_dataArray addObject:model];
        
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    TopImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"123" forIndexPath:indexPath];
    
    cell = [cell init];
    
    cell.model = _dataArray[indexPath.row];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.navigationController pushViewController:[[MovieController alloc] init] animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
